import axios from "axios"
import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex)

import user from "./modules/user.js"
import classObj from "./modules/classObj.js"
// 价格
import getbooks from "./modules/getbooks.js"
// 循环
import Actions from "./modules/Actions.js"
// 实例化vuex
export default new Vuex.Store({
  state: {
    // class: "小彭",
    // userInfo: {
    //   name: 'tom',
    //   skills: ['抖音', 'B站', '美团'],
    //   address: '武汉黑马',
    //   logo: 'https://vuejs.org/images/logo.svg'
    //   //  https://www.runoob.com/wp-content/uploads/2016/02/react.png
    // },
    // books: [{
    //     "name": "javasript技术内幕",
    //     "price": 100,
    //     "img": "https://img3m7.ddimg.cn/64/26/29120617-1_u_8.jpg"
    //   },
    //   {
    //     "name": "数学之美",
    //     "price": 44,
    //     "img": "https://img3m2.ddimg.cn/18/30/28538352-1_b_5.jpg"
    //   },
    //   {
    //     "name": "认知天性",
    //     "price": 40,
    //     "img": "https://img3m3.ddimg.cn/74/33/1732997153-3_u_3.jpg"
    //   }
    // ],
    newbooks: []
  },
  mutations: {
    //   1.更改state里面的值,必须通过mutations,
    // 2.修改mutations的值,必须通过commit
    //   {newlogo,two}解构
    // setLogo: function (state, {
    //   newlogo,
    //   two
    // }) {
    //   state.userInfo.logo = newlogo
    //   console.log(two);

    // },
    setbooks(state, newboos) {
      state.newbooks = newboos
    }
  },
  getters: {
    // sum(state) {
    //   return state.books.reduce((totle, item) => totle += item.price, 0)
    // }
  },
  //   actions发起异步请求
  actions: {
    async Agetbooks({
      commit
    }) {
      let res = await axios({
        url: "https://www.fastmock.site/mock/37d3b9f13a48d528a9339fbed1b81bd5/book/api/books",
      })
      //   console.log(res);
      //   context里面有一个commit方法,
      commit("setbooks", res.data.data)
    }
  },
  modules: {
    user,
    classObj,
    getbooks,
    Actions
  }
})
